from datetime import datetime
import connect
conn = connect.connect()

# 创建一个游标对象
cursor = conn.cursor()
# 查询数据
def select_all_data(limit, offset):
    select_query = "SELECT * FROM unresolved_sentence LIMIT %s OFFSET %s"
    cursor.execute(select_query, (limit, offset))

    # 获取列名
    column_names = [desc[0] for desc in cursor.description]
    
    # 获取所有行的数据
    rows = cursor.fetchall()
    
    # 将结果构建成字典列表
    result = [dict(zip(column_names, row)) for row in rows]
    
    return result

# 插入数据
def insert_data(data):
    values = list(data.values())
    insert_query = "INSERT INTO unresolved_sentence (ip, sentence, create_time, update_time) VALUES (%s, %s, %s, %s)"
    try:
        cursor.execute(insert_query, values)
        conn.commit()
        print("插入成功")
    except:
        print("插入失败")

# 更新数据
def update_data(id, reason):
    update_query = "UPDATE unresolved_sentence SET reason = %s WHERE id = %s"
    try:
        cursor.execute(update_query, (reason, id))
        conn.commit()
        print("更新成功")
    except:
        print("更新失败")
# obj = {
#     "ip": "127.0.0.1",
#     "sentence": "酒桶和女警都可以打下路吗？",
#     "create_time": datetime.now(),
#     "update_time": datetime.now()
# }
# insert_data(obj)
# update_data(1, "打下路被识别为一个词，词性标注为v")
# data = select_all_data(1, 0)
# print(data)